package pure.utils 
{
	import flash.display.*;
	import flash.filters.*;
	
	/**
	 * 图像(实用类)
	 * @author Des
	 * 
	 */
	
public class DesG
{
	
	public static const RED:uint = 0xf41e36;	// 红(自然)
	public static const GREEN:uint = 0x18d300;	// 绿(自然)
	public static const GREEN_PURE:uint = 0x39FF0B;	// 绿(纯)
	public static const GREEN_DEEP:uint = 0x2a4209;	// 绿(深)
	public static const BLUE_LITE:uint = 0x00F0FF;	// 兰
	
	public static const YELLOWCOLOR:uint=0xFFFF00;	// 黄
	public static const ORANGE:uint = 0XFF9C00;	// 橙
	public static const GRAY:uint = 0x999999;	// 灰
	public static const PINK:uint = 0xCC6666;	// 粉(自然)
	public static const BROWN:uint = 0x2b1209;	// 棕(深)
	public static const LEMON:uint = 0xFFEB7B;	// 柠檬

	
	/**
	 * 灰色滤镜
	 * @param display		显示对象
	 */		
	public static function applyGrayFilter(display:DisplayObject):void
	{
		var rLum:Number = 0.3086;
		var gLum:Number = 0.6094;
		var bLum:Number = 0.0820;
		display.filters = [new ColorMatrixFilter([rLum, gLum, bLum, 0, 0, rLum, gLum, bLum, 0, 0, rLum, gLum, bLum, 0, 0, 0, 0, 0, 1, 0])];
	}
		
	/**
	 * 彩色BitmapData→黑白BitmapData
	 * @param source		 位图数据
	 */		
	public static function grayFilter(source:BitmapData):BitmapData
	{
		var bitmap:Bitmap = new Bitmap(source);
		applyGrayFilter(bitmap);
		var bitmapData:BitmapData = new BitmapData(bitmap.width, bitmap.height, true, 0x00ffffff);
		bitmapData.draw(bitmap);
		return bitmapData;
	}
	
	/**
	 * 画扇形
	 * @param display 		绘制对象
	 * @param x				x坐标
	 * @param y				y坐标
	 * @param r				半径
	 * @param angle			角度
	 * @param startAngle 	起始角度
	 * @param color			颜色
	 */
	public static function drawSector(display:Shape,x:Number,y:Number,r:Number,angle:Number=90,startAngle:Number=0,color:Number=0xff0000):void 
	{
		angle = (Math.abs(angle) > 360)?360:angle;
		var n:Number = Math.ceil(angle / 45);	//[1-8]
		var radian:Number = (angle / n) * Math.PI / 180;
		var startRadian:Number = startAngle * Math.PI / 180;
		
		//画线
		display.graphics.beginFill(color,.4);//
		display.graphics.moveTo(x,y);
		display.graphics.lineTo(x + r * Math.cos(startRadian), y + r * Math.sin(startRadian));
		
		//
		for (var i:int = 1; i <= n; i++)
		{
			startRadian+=radian;
			var angleMid:Number=startRadian-radian/2;
			var bx:Number=x+r/Math.cos(radian/2)*Math.cos(angleMid);
			var by:Number=y+r/Math.cos(radian/2)*Math.sin(angleMid);
			var cx:Number=x+r*Math.cos(startRadian);
			var cy:Number=y+r*Math.sin(startRadian);
			display.graphics.curveTo(bx,by,cx,cy);
		}
		if (angle != 360) 
		{
			display.graphics.lineTo(x,y);
		}
		display.graphics.endFill();
	}	
	
	/**
	 * 生成矩形
	 * @param w 			长
	 * @param h 			宽
	 * @param fillColor		填充颜色
	 * @param ellipse		四角半径
	 * @param thickness		边线粗细
	 * @param lineColor		边线颜色
	 */
	public static function createRect(w:Number, h:Number,
									fillColor:uint = 0x2b1209,
									alpha:Number = 1,
									ellipse:int = 8,
									thickness:Number = 1.2,
									lineColor:uint = 0):Shape
	{
		var rect:Shape = new Shape();
		rect.graphics.beginFill(fillColor,alpha);
		rect.graphics.lineStyle(thickness, lineColor, alpha);
		rect.graphics.drawRoundRect(0, 0, w, h, ellipse, ellipse);
		return rect;
	}
	
	/**
	 * 生成直线
	 * @param length		长度
	 * @param thickness		粗细
	 * @param lineColor		颜色
	 **/
	public static function createLine(length:Number, isHorizontal:Boolean,
									lineColor:uint = 0,
									alpha:Number = 1,
									thickness:Number = 1.2):Shape
	{
		var line:Shape=new Shape();
		line.graphics.lineStyle(thickness, lineColor, alpha);
		if (isHorizontal)
		{
			line.graphics.moveTo(0 , 0);
			line.graphics.lineTo(length , 0);
		}
		else
		{
			line.graphics.moveTo(0 , 0);
			line.graphics.lineTo(0,length);
		}
		return line
	}

}
}